package cn.com.lms.business.core.util;

import com.bnzj.core.util.FbConstant;
import com.bnzj.core.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
public class ServletUtils {
	
	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

    public static String getIpAddress() {
        return getIpAddress(getRequest());
    }
    
    public static String getCurrentView() {
        String path = ServletUtils.getRequest().getRequestURI();
        path = path.replaceFirst("/", "");
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }
    

    public static Pageable initPage() {
        return initPage(getRequest(), null);
    }
	
	public static Pageable initPage(HttpServletRequest httpRequest) {
		return initPage(httpRequest, null);
	}
	
	public static Pageable initPage(HttpServletRequest httpRequest, Sort sort) {
		return initPage(httpRequest.getParameter("limit"), httpRequest.getParameter("offset"), sort);
	}

    public static Pageable initPage(String limit, String offset, Sort sort) {
        if ("undefined".equals(limit) || "undefined".equals(offset)) {
            limit = null;
            offset = null;
        }
        int start = StringUtils.isEmpty(offset) ? 0 : Integer.parseInt(offset);
        int pageSize = StringUtils.isEmpty(limit) ? 10 : Integer.parseInt(limit);
        int current = start / pageSize;
        if (sort == null) {
            sort = Sort.unsorted();
        }
        return PageRequest.of(current, pageSize, sort);
    }
    
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    
    /**
     * 编码文件名称
     * @param fileName
     * @return
     */
    public static String encodeFileName(String userAgent, String fileName) {
        String newName = fileName;
        try {
            if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("like Gecko")) {
                newName = URLEncoder.encode(fileName, FbConstant.GLOBAL_ENCODE);
            } else {
                newName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            }
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
        }
        return newName;
    }
}
